//
//  LHRadioDownloadManager.m
//  EPA
//
//  Created by 卢浩 on 16/4/27.
//  Copyright © 2016年 卢浩. All rights reserved.
//

#import "LHRadioDownloadManager.h"
#import "LHRadioDownloadTask.h"
#import "LHRadioDetailModel.h"

@implementation DownloadManagerTask

- (NSString *)description
{
    return self.model.title;
}

@end

@interface LHRadioDownloadManager ()

@property (nonatomic,strong) NSMutableArray *taskArr;

@property (nonatomic,strong) NSString *musicName;

@end

@implementation LHRadioDownloadManager

+ (instancetype)shareManager{
    static LHRadioDownloadManager *manager = nil;
    @synchronized (self) {
        if (manager == nil) {
            manager = [[LHRadioDownloadManager alloc]init];
            manager.array = [NSMutableArray array];
            manager.taskArr = [NSMutableArray array];
        }
    }
    return manager;
}

- (NSMutableArray *)tempDownloadArr{
    if (_tempDownloadArr == nil) {
        _tempDownloadArr = [NSMutableArray array];
        NSString *tempDownloadArrPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"tempDownloadPath.plist"];
        [_tempDownloadArr addObjectsFromArray:[NSArray arrayWithContentsOfFile:tempDownloadArrPath]];
//        NSLog(@"%@",_tempDownloadArr);
    }
    return _tempDownloadArr;
}

- (NSMutableArray *)downloadArr{
    _downloadArr = [NSMutableArray array];
    NSString *downloadArrPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"downloadPath.plist"];
    [_downloadArr addObjectsFromArray:[NSArray arrayWithContentsOfFile:downloadArrPath]];
    return _downloadArr;
}

//static NSInteger count = 0;

- (void)addDownloadTaskWithModel:(LHRadioDetailModel *)model ProgressBlock:(ProgressBlock)proBlock CompleteBlock:(CompleteBlock)comBlock{
    BOOL isContail = NO;
    for (DownloadManagerTask *task in self.array) {
        if ([task.model.title isEqualToString:model.title]) {
            isContail = YES;
        }
    }
    //包含
    if (isContail) {
        return;
    }
    
    //下载
    LHRadioDownloadTask *downloadTask = [[LHRadioDownloadTask alloc]init];
    [downloadTask downloadUrl:[NSURL URLWithString:model.musicUrl] ProgressBlock:proBlock CompleteBlock:comBlock];
    
    //添加到数组中
    DownloadManagerTask *downloadManagerTask = [[DownloadManagerTask alloc] init];
    downloadManagerTask.model = model;
    downloadManagerTask.downloadTask = downloadTask;
    [self.array addObject:downloadManagerTask];
    [self.taskArr addObject:downloadManagerTask];
    
    NSString *mp3Name  = [model.musicUrl stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
    mp3Name= [mp3Name stringByReplacingOccurrencesOfString:@":" withString:@"_"];
    
    self.musicName = [model.title stringByAppendingString:[NSString stringWithFormat:@"|%@",mp3Name]];
//    NSLog(@"%@",musicName);
    
    [self.tempDownloadArr addObject:self.musicName];
    [self writeTempDownloadArr];
    
//    //接收通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeTaskAndWritDidDownloadArr) name:[NSString stringWithFormat:@"%@downloadFinsh",mp3Name] object:nil];
}

- (void)removeTaskAndWritDidDownloadArr{
//    [self.array removeObject:self.taskArr[count]];
    
    [self writeDownloadArrWithMp3Name:nil Title:nil];
    
//    count++;
}

- (void)writeTempDownloadArr{
    NSString *tempDownloadArrPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"tempDownloadPath.plist"];
    [self.tempDownloadArr writeToFile:tempDownloadArrPath atomically:YES];
}

- (void)writeDownloadArrWithMp3Name:(NSString *)mp3Name Title:(NSString *)title{
    NSString *downloadArrPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"downloadPath.plist"];
    if (title) {
        NSString *musicName = [title stringByAppendingString:[NSString stringWithFormat:@"|%@",mp3Name]];
        if (![self.downloadArr containsObject:musicName]) {
            NSMutableArray *tempArr = [NSMutableArray array];
            [tempArr addObjectsFromArray:self.downloadArr];
            [tempArr addObject:musicName];
            [tempArr writeToFile:downloadArrPath atomically:YES];
        }
    }else{
        NSMutableArray *tempDLArr = [NSMutableArray array];
        [tempDLArr addObjectsFromArray:self.tempDownloadArr];
        for (DownloadManagerTask *task in self.array) {
            NSString *mp3Name  = [task.model.musicUrl stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
            mp3Name= [mp3Name stringByReplacingOccurrencesOfString:@":" withString:@"_"];
            NSString *musicName = [task.model.title stringByAppendingString:[NSString stringWithFormat:@"|%@",mp3Name]];
            if ([tempDLArr containsObject:musicName]) {
                [tempDLArr removeObject:musicName];
            }
        }
        for (NSString *musicName in tempDLArr) {
            if (![self.downloadArr containsObject:musicName]) {
                NSMutableArray *tempArr = [NSMutableArray array];
                [tempArr addObjectsFromArray:self.downloadArr];
                [tempArr addObject:musicName];
                [tempArr writeToFile:downloadArrPath atomically:YES];
            }
        }
    }
}

//当前下载任务个数
- (NSInteger)count{
    return self.array.count;
}

//取标题
- (NSString *)getTitleWithIndex:(NSInteger)index{
    return [self.array[index] description];
}

//设置block
- (void)setProgressBlock:(ProgressBlock)proBlock CompleteBlock:(CompleteBlock)comBlock Index:(NSInteger)index{
    __weak DownloadManagerTask *task = self.array[index];
    [task.downloadTask setProgressBlock:proBlock];
    [task.downloadTask setCompleteBlock:^(NSString *path) {
        
        if ([self.array containsObject:task]) {
            [self.array removeObject:task];
        }
        
        NSString *mp3Name  = [task.model.musicUrl stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
        mp3Name= [mp3Name stringByReplacingOccurrencesOfString:@":" withString:@"_"];
        [self writeDownloadArrWithMp3Name:mp3Name Title:task.model.title];
        
        //发送通知
        [[NSNotificationCenter defaultCenter] postNotificationName:@"downloadReloadData" object:nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"didDownloadReloadData" object:nil];
    }];
}

@end
